package com.example.demo2.util;

import com.example.demo2.entity.Result;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.NavigableMap;
import java.util.TreeMap;

public class ResultIndex {
    private NavigableMap<BigDecimal, List<Result>> resultIndex = new TreeMap<>();

    public void addResult(Result result) {
        BigDecimal key = new BigDecimal(String.valueOf(result.getResult()));
        resultIndex.computeIfAbsent(key, k -> new ArrayList<>()).add(result);
    }

    public List<Result> findResultsInRange(BigDecimal lowerBound, BigDecimal upperBound) {
        List<Result> matchingResults = new ArrayList<>();
        resultIndex.subMap(lowerBound, true, upperBound, true).values().forEach(matchingResults::addAll);
        return matchingResults;
    }
}
